/**
 * Created by 炜 on 2016/7/16.
 */
angular.module("starter.myController", [])
  .controller("MyCtrl", function ($scope, $user) {
    $user.getCurrentUser().executeSuccess(function (user) {
      $scope.username = user.userName;
      $scope.userPhone = user.userPhone;
    })
  })
  .controller("MySettingsCtrl", function ($scope, $state, $cordovaDialogs, $ionicHistory, $dfCache, $user, $ionicPopup) {
    // 退出登录
    $scope.logout = function () {

      $cordovaDialogs.confirm('确认退出?', '退出', ['取消', '退出']).then(function (buttonIndex) {
        if (buttonIndex == 2) {
          //获取用户当前用户
          $user.getCurrentUser().executeSuccess(function (user) {
            //判断是否为自动登录
            if (user.autoLogin) {
              try {
                //更改为false
                user.autoLogin = false;
                user.currentLogin = false;

                //保存user
                $user.saveUserWithoutAutoLogin(user);

                $user.clearUser();

                window.plugins.jPushPlugin.setAlias("");
              } catch (e) {
                console.log(e);
              }
            }
            //清除历史并跳转到login页面
            $ionicHistory.clearCache().then(function () {
              $state.go("login");
            });
          });
        }
      });
    }
  })
  .controller("MyAccountCtrl", function ($scope, $state, $cordovaDialogs, $ionicHistory, $dfCache, $user, $ionicPopup) {

  })
  .controller("MyAccountPhonePasswordCtrl", function ($scope, $loading, $state, $cordovaDialogs, $ionicHistory, $dfCache, $user, $ionicPopup) {

    var Currentuser = {};
    // 获取当前user信息
    $user.getCurrentUser().executeSuccess(function (user) {
      Currentuser = user;
    });
    // 获取页面上ng-model输入的数据
    $scope.user = {
      surepassword: ""
    };

    // 点击方法
    $scope.resetandgo = function () {
      if ($scope.user.surepassword == Currentuser.password) {
        $state.go('tab.my-account-phone');
      }else if ($scope.user.surepassword == ""){
        $loading.showError("请输入密码!");
      } else {
        $loading.showError("密码输入错误,请重新输入!");
      }

    }

  })

  .controller("MyAccountPhoneCtrl", function ($scope, $timeout, $loading, $state, $dfHttp, $cordovaDialogs, $ionicHistory, $dfCache, $user, $ionicPopup) {

    var Currentuser = {};
    $scope.user = {};
    $user.getCurrentUser().executeSuccess(function (user) {
      Currentuser = user;
    });

    $scope.getToken = function (userPhone, businessType) {
      if ($scope.user.newMobile == "") {
        $loading.showError("手机号不能为空!");
      } else {
        $dfHttp.get('infor/user/getSecurityCode.json', {
          "userPhone": $scope.user.newMobile,
          "businessType": -8
        }).success(function (data) {
          if (data.code == "SUCCESS") {
            console.log("11111");
            $loading.showSuccess(data.msg);
          } else {
            $loading.showError(data.msg);
          }
        }).error(function () {
          $loading.showError("网络有点小问题，请稍后再试");
        })
      }
    }


    $scope.resetandgoback = function (newMobile, mobile, token) {

      if (!(/^1[34578]\d{9}$/.test($scope.user.newMobile))) {
        $loading.showError("请输入正确的手机号!");
      } else if (Currentuser.userPhone == $scope.user.newMobile) {
        $loading.showError("手机号不能相同!");
      } else if ($scope.user.token == "") {
        $loading.showError("验证码不能为空!");
      } else {
        $dfHttp.post('infor/user/userUpdatePhone.json', {
          "newMobile": $scope.user.newMobile,
          "mobile": Currentuser.userPhone,
          "token": $scope.user.token
        }).success(function (data) {
          if (data.code == "SUCCESS") {
            $loading.showSuccess(data.msg);
            $timeout(function(){
              $state.go("login");
            }, 500);
            window.plugins.jPushPlugin.setAlias($scope.user.newMobile);
          } else {
            $loading.showError(data.msg);
          }
        }).error(function () {
          $loading.showError("网络有点小问题，请稍后再试");
        })
      }
    }

  })
  .controller("MyAccountPasswordCtrl", function ($scope, $timeout, $dfHttp, $loading, $user, $state, $cordovaDialogs, $ionicHistory, $dfCache, $user, $ionicPopup) {

    var CurrentUser = {};
    $scope.user = {
      newpassword: "",
      password: "",
      oldpassword: ""
    };
    $user.getCurrentUser().executeSuccess(function (user) {
      CurrentUser = user;
    })
    $scope.resetandback = function () {
      if ($scope.user.newpassword == "") {

        $loading.showError("密码不能为空!");

      } else if ($scope.user.newpassword.length < 6) {

        $loading.showError("请输入至少6位密码!");

      } else if ($scope.user.newpassword != $scope.user.surepassword) {

        $loading.showError("两次密码输入不一致!");

      } else {
        $dfHttp.post('infor/user/userUpdatePassWord.json', {
          "userId": CurrentUser.userId,
          "password": $scope.user.oldpassword,
          "newPassword": $scope.user.newpassword
        }).success(function (data) {
          if (data.code == "SUCCESS") {
            $loading.showSuccess(data.msg);
            $timeout(function(){
              $state.go("login");
            }, 500);
          } else {
            $loading.showError(data.msg);
          }
        }).error(function () {
          $loading.showError("网络有点小问题，请稍后再试");
        })
      }

    }

  })
  .controller("MyMessagesCtrl",function ($scope, $user, $dfHttp, $loading) {


    // 获取当前user信息
    $scope.currecntUser = $user.getCurrentUserSync();

    getMessage($scope.currecntUser.userId);

    $scope.messageList = {};
    function getMessage (userId) {
      $dfHttp.get('api/others/getMessgePush.json', {
        "userId": $scope.currecntUser.userId
      }).success(function (data) {
        if (data.code == "SUCCESS") {
          $scope.messageList = data.data;
          console.log($scope.messageList);
        } else {
          $loading.showError(data.msg);
        }
      }).error(function () {
        $loading.showError("网络有点小问题，请稍后再试");
      })
    }
  });
